<?php 




    function db_connect($db_name,$host='127.0.0.1',$db_user='root',$db_pwd='root',$charset='utf8'){

			//连接到数据库
			$link=mysqli_connect($host,$db_user,$db_pwd,$db_name);
			if (!$link) {
			echo '<script> alert ("数据库连接失败");history.go(-1);</script>';die;

			//         //history.go(-1)  是js用来反回上一个页面的写法
			}

			//编写链接数据库识别的中文编码
			mysqli_query($link,'set names '.$charset);
			
			return $link;

}

      // 删除语句封装
   function del($table,$where=array()){
       
     	if(is_array($where)){
       // 转为字符串
     		 $str= implode(',', $where);
     		 $where="in ($str)";

     	}else{
     		$where ='= '.$where; 
     	}
     $sql="delete from `$table` where id $where";
 
     	mysqli_query($GLOBALS['link'], $sql);

     	return mysqli_affected_rows($GLOBALS['link']);

}

 // 编辑语句代码封装
function edit($table,$where,$data=array()){


  


      $field= '';
      foreach($data as $k=>$v){
        $field.=" `$k` = '$v',"; //第一次循环 $field = `username`='steve',
        // $field.="`$k` = '$v',"; //第2次循环 $field = `username`='steve',`sex`='1',
        // $field.="`$k` = '$v',"; //第一次循环 $field =  `username`='steve',`sex`='1',`age`='20',

         
      }
         // 字符串截取
      $field = substr($field,0,-1);


   $sql = "update `$table` set $field where `id`=$where";
   //编写执行语句
      mysqli_query($GLOBALS['link'], $sql);

      return mysqli_affected_rows($GLOBALS['link']);

}


     // 添加语句代码封装
  function add($table,$where=array()){

        $v ="'".implode("','",array_values($where))."'";
       
        $key =  "`".implode("`,`",array_keys($where))."`";
       //编写插入语句 
         // $sql = "insert into `admin`(`username`,`password`,`sex`,`age`,`url`)values('$username','$password','$sex','$age','$url')";
 $sql = "insert into `$table`($key)values($v)";
   // echo '<br>';
    		//编写执行语句
       
    		mysqli_query($GLOBALS['link'], $sql);

     	return mysqli_affected_rows($GLOBALS['link']);

}



      // 单次查询代码封装
  function getSelectByOne($table,$where,$field='*'){

      	if(is_array($where)){
      		 $str= '';
      		 $i = 0;
      		 foreach ($where as $k => $v) {

      		 	if($i==count($where)-1){
      		 	 $str .="`$k` = '$v'";
      		 	}else{
      		 	 $str .="`$k` = '$v'  and";
      		 }
      		 $i++;
      	}
     		$where = $str;
     }


  $sql = "select $field from $table where $where";

      	$result = mysqli_query($GLOBALS['link'], $sql);

     	return mysqli_fetch_assoc($result);
 }




// 查询多条语句代码封装
  function getSelect($table,$where=1,$order='',$limit='',$field='*'){
         $where1= '';
		
  	     if(!empty($order)){

  	     	$where1.= 'order by '.$order;
  	     }
         if(!empty($limit)){
          $where1.= " limit $limit ";
          }

 $sql="select $field from `$table` where $where $where1";
       $result = mysqli_query($GLOBALS['link'],$sql);

         while($arr = mysqli_fetch_assoc($result)){

         	$cate[] = $arr;
         }



          return $cate;

  }
